package com.example.prograssbardemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.SeekBar;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private int progressStatus = 0;
    private ProgressBar progress = null;
    private ProgressHandler handler = null;
    private ImageView imgView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_PROGRESS);

        setContentView(R.layout.activity_main);
        progress = (ProgressBar) findViewById(R.id.progress);
        progress.setVisibility(View.INVISIBLE);
        handler = new ProgressHandler();
        Button btnStart = (Button) findViewById(R.id.btn_start);
        /* 点击按钮，启动下载任务
         * 下载任务中发送消息
         * Handler中处理消息，将进度值在ProgressBar上更新
         */
        btnStart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                progressStatus = 0;
                progress.setVisibility(View.VISIBLE);
                DownloadThread thread = new DownloadThread();
                thread.start();
            }
        });

        imgView = (ImageView) findViewById(R.id.img);
        final SeekBar seekBar = (SeekBar) findViewById(R.id.seek);
        final RatingBar ratingBar = (RatingBar) findViewById(R.id.rating);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                float alpha = 1 - i / 100.0f;
                imgView.setAlpha(alpha);
                ratingBar.setRating(i / 20.0f);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                float alpha = 1 - v / 5;
                imgView.setAlpha(alpha);
                seekBar.setProgress((int) (v * 20));
            }
        });
    }

    // 模拟下载进程
    class DownloadThread extends Thread {

        @Override
        public void run() {
            super.run();
            while (progressStatus <= 100) {
                try {
                    Thread.sleep(200);
                    progressStatus++;


                    ////////////////////
                    Message msg = new Message();
                    msg.what = 101;
                    msg.arg1 = progressStatus;
                    handler.sendMessage(msg);
                    /////////////////////
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // Handler
    class ProgressHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 101) {
                progress.setProgress(msg.arg1);
                if (msg.arg1 >= 100)
                    progress.setVisibility(View.GONE);
            }

        }
    }
}